 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.sql.SQLException;
import model.User;


/**
 *
 * @author Adriano
 */
public class UserDAO implements DAO {

    private final DataSource dataSource;

    public UserDAO() {
        dataSource = new DataSource();
    }
@Override
    public void create(Object o) {
        try {
           
            String sql = "INSERT INTO user VALUES (null,?,?,SHA2(?,256),?,?)";
            //String sql = "INSERT INTO user VALUES (null,?,?,?,?,?)";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            User user = (User) o;
          
            ps.setString(1, user.getName());
            ps.setString(2, user.getEmail());
            ps.setString(3, user.getPassword());
            ps.setString(4, user.getLstprofile());
            ps.setString(5, user.getLstStatus());

            int res = ps.executeUpdate();
            if (res == 0) {
                System.err.println("USERDAO.CREATE - Nenhum usuário criado !");
            }
        } catch (SQLException e) {
            System.err.println("USERDAO.CREATE  - " + e.getMessage());
        }
    }

    @Override
    public void delete(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void update(Object o) {
        try {

            String sql = "UPDATE  user SET name=? ,email= ? , profile=? , status=?  WHERE id = ? ";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            User query = (User) o;

            ps.setString(1, query.getName());
            ps.setString(2, query.getEmail());
//            ps.setString(3, query.getPassword());
            ps.setString(3, query.getLstprofile());
            ps.setString(4, query.getLstStatus());
            ps.setInt(5, query.getId());

            ps.executeUpdate();

        } catch (SQLException e) {
            System.err.println("UPDATEUSER - " + e.getMessage());
        }

    }

    public Object read(Object o) {

        try {

            String sql = "SELECT * FROM user WHERE email = ? AND password =SHA2(?,256) AND status='Ativo'";
            //String sql = "SELECT * FROM user WHERE email = ? AND password =? AND status='Ativo'";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            User query = (User) o;

            ps.setString(1, query.getEmail());
            ps.setString(2, query.getPassword());
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {

                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setEmail(rs.getString("email"));
                user.setPassword(rs.getString("password"));
                user.setLstprofile(rs.getString("profile"));
                user.setLstStatus(rs.getString("status"));

                return user;
            }
        } catch (SQLException e) {
            System.err.println("USERDAO.READ - " + e.getMessage());
        }
        return null;
    }

    public Object find(String key) {

        try {

            String sql = "SELECT * FROM user WHERE email = ?";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            ps.setString(1, key);

            ResultSet rs = ps.executeQuery();
          
            if (rs.next()) {
                User user = new User();
                
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setEmail(rs.getString("email"));
                user.setPassword(rs.getString("password"));
                user.setLstprofile(rs.getString("profile"));
                user.setLstStatus(rs.getString("status"));
                System.out.println("DEBUG " + ps.toString());
                System.out.println(rs.getString("email"));
                return user;
            }
        } catch (SQLException e) {
            System.err.println("BUSCAUSERDAO.READ - " + e.getMessage());
        }
        return null;

    }

    public Object findAll() {
        try {
            
            String sql = "SELECT * FROM user";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            ArrayList<User> listaUser = new ArrayList<User>();

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {

                User user = new User();

                user.setId(rs.getInt("User.id"));
                user.setName(rs.getString("User.name"));
                user.setEmail(rs.getString("User.email"));
                user.setLstprofile(rs.getString("User.profile"));
                user.setLstStatus(rs.getString("User.status"));
                               
                listaUser.add(user);

            }
            return listaUser;

        } catch (SQLException e) {
            System.err.println("USERDFINDALL.READ - " + e.getMessage());
            return null;
        }

    }

    public void closeDAO() {
        dataSource.closeConnection();
    }

}
